import { sample } from "lodash-es"
import { Character, CharacterData } from "./game/Character"
import { createBattle } from "./utils/battle"
import emitter from "./utils/events"
import { addAction } from "./utils/request"
import { useBattle } from "./utils/use"
import { skillTable } from "./data/SkillTable"



async function main() {
  await createBattle({
    playerB: {
      name: "侧翼步兵",
      id: 1026452,
      global: {
        counter: [
        ],
        type: 1,
        desid: [
          400000,
        ],
        buffs: [
        ],
        player: 1026452,
        auras: [
        ],
        id: 33,
        templet: {
          race: 1,
          power: 6,
          mana: 2,
        },
        card_id: 100000,
        currentHP: 6,
        currentMP: 2,
        maxHP: 1,
        maxMp: 1,
        atk: [1, 1]
      },
      vocation: 4,
      mana: 2,
    },
    playerA: {
      name: "帝国皇帝",
      id: 3154,
      global: {
        type: 1,
        buffs: [
        ],
        player: 3154,
        auras: [
        ],
        id: 1,
        templet: {
          race: 1,
          power: 6,
          mana: 2,
        },
        card_id: 100000,
        currentHP: 6,
        currentMP: 2,
        maxHP: 1,
        maxMp: 1,
        atk: [1, 1]
      },
      vocation: 4,
    }
  })
  const battle = useBattle()
  const card1 = new CharacterData(10001)
  const Character1 = battle.value.createCharacter(battle.value.playerA, undefined, card1)

  const card2 = new CharacterData(10002)
  const Character2 = battle.value.createCharacter(battle.value.playerB, undefined, card2)
  const group1 = {
    name: '玩家1',
    characters: [Character1]
  }
  const group2 = {
    name: '玩家2',
    characters: [Character2]
  }
  while (group1.characters.some(item => !item.dead) && group2.characters.some(item => !item.dead)) {
    await emitter.emitSerial('timing', ['beforernd'])
    for (let Character of group1.characters) {
      const canUseSkills = Character.skills.filter(item => item.cd === -1 || item.cd >= skillTable[item.skillId].cd)
      const target = sample(group2.characters.filter(item => !item.dead))
      if (canUseSkills.length) {
        const skill = sample(canUseSkills)
        if (skill) {
          await skillTable[skill.skillId].func.call({ character: Character, skill }, target)
          skill.cd = 0
          console.log(`${Character.name} 对 ${target?.name} 使用了技能 ${skillTable[skill.skillId].name}`);
        }
      } else {
        await emitter.emitSerial('startattack', [Character, target, 1])
      }
    }
    for (let Character of group2.characters) {
      const canUseSkills = Character.skills.filter(item => item.cd === -1 || item.cd >= skillTable[item.skillId].cd)
      const target = sample(group1.characters.filter(item => !item.dead))
      if (canUseSkills.length) {
        const skill = sample(canUseSkills)
        if (skill) {
          await skillTable[skill.skillId].func.call({ character: Character, skill }, target)
          skill.cd = 0
          console.log(`${Character.name} 对 ${target?.name} 使用了技能 ${skillTable[skill.skillId].name}`);
        }
      } else {
        await emitter.emitSerial('startattack', [Character, sample(group1.characters.filter(item => !item.dead)), 1])
      }
    }
    await emitter.emitSerial('timing', ['afterrnd'])
  }
  const winGroup = group1.characters.some(item => !item.dead) ? group1 : group2
  console.log('获胜的玩家是', winGroup.name);
}
const battle = useBattle()
emitter.on('timing', async ([timing, para]) => {
  // console.log('this timing is', timing);
  await battle.value.refreshAuras()
  await battle.value.activateEffects(timing, para)
  await emitter.emitSerial('afterTiming', [timing])
  if (timing === 'afterrnd') {
    Object.values(battle.value.characters).forEach(item => {
      if (item) {
        item.skills?.map(item => {
          if (item.cd > -1) {
            item.cd++
          }
        })
      }
    })
  }
})

emitter.on('beforeAction', async ([name, action, { send }]) => {
  if (name === 'attack') {
    await emitter.emitSerial('timing', ['beforeattack', { ...action, obj: battle.value.characters[action.obj.id] }])
  }
})

emitter.on('afterAction', async ([name, action, { send }]) => {
  if (name === 'attack') {
    await emitter.emitSerial('timing', ['afterattack', {
      ...action,
      obj: battle.value.characters[action.obj.id],
      sub: battle.value.characters[action.sub.id]
    }]
    )
  }
})


emitter.on('startattack', async ([enemy, minion, field]: [Character | undefined, Character | undefined, number]) => {
  if (!enemy || !minion) return
  await addAction.attack(enemy, minion)
})

main()
